<?php

class Application_Model_TagsDb
{
    protected $_table_class;

    public function __construct()
    {
        $this->_table_class='Application_Model_DbTable_Tags';
    }
    
    /**
     * Returns an array with tag objects
     *
     * @return array
     */
    public function fetch($url_alias='',$language=null)
    {
        if (empty($language)) $language=Zend_Registry::get('language')->getLocale();
        
        $tags=array();
        
        $tag_tbl=new $this->_table_class();
        
        
     //   $tag_rows=$tag_tbl->fetchAll();
     
        if (empty($url_alias))
        {
            $select=$tag_tbl->select()
                            ->distinct()
                            ->from('tags','tag')
                            ->where('language=?',$language)
                            ->limit(50);
        } else
        {
            $select=$tag_tbl->select()
                            ->distinct()
                            ->from('tags','tag')
                            ->where('language=?',$language)
                            ->where("(url_alias=? or url_alias='')",$url_alias)
                            ->limit(50);    
        }
        
        $st=$select->query();
        $tag_rows=$st->fetchAll();
        
        foreach ($tag_rows as $row)
        {
            $tag=new Application_Model_Tags($row);
            
            $tags[]=$tag;
        }
        
        return $tags;
    }
    
    /**
     * Returns tag object by ID
     *
     * @return object
     */
    public function fetchTagById($id)
    {
        $tag_tbl=new $this->_table_class();

        $select=$tag_tbl->select()
                        ->from('tags')
                        ->where('id=?',$id);
    
        $st=$select->query();
        $row=$st->fetch();

        $tag=new Application_Model_Tags($row);
        
        return $tag;
    }
    
    /**
     * Returns tag object by node ID
     *
     * @param int $nid
     * @param int $limit
     * 
     * @return object
     */
    public function fetchTagsByNodeId($nid,$limit=null)
    {
        $tags=array();
        
        $tag_tbl=new $this->_table_class();

        if (!empty($limit))
        {
            $select=$tag_tbl->select()
                                ->distinct()
                                ->from('tags')
                                ->where('nid=?',$nid)
                                ->limit($limit);
        } else
        {
            $select=$tag_tbl->select()
                                ->distinct()
                                ->from('tags')
                                ->where('nid=?',$nid);
        }
    
        $st=$select->query();
        $tag_rows=$st->fetchAll();
        
        foreach ($tag_rows as $row)
        {
            $tag=new Application_Model_Tags($row);
            
            $tags[]=$tag;
        }
        
        return $tags;
    }
    
    /**
     * Returns tag object by clause
     *
     * @param array $clause
     * @param bool $distinct
     * @param int $limit
     * 
     * @return object
     */
    public function fetchTagsByClause($clause,$distinct=false,$limit=null)
    {
        $tags=array();
        
        $tag_tbl=new $this->_table_class();

        $select=$tag_tbl->select();
        
        if ($distinct)
        {
            $select->distinct();
        }
                                
        $select->from('tags');
        
        foreach($clause as $where=>$params)
        {
            $select->where($where,$params);
        }
                               
        if (!empty($limit))
        {
            $select->limit($limit);
        }
    
        $st=$select->query();
        $tag_rows=$st->fetchAll();
        
        foreach ($tag_rows as $row)
        {
            $tag=new Application_Model_Tags($row);
            
            $tags[]=$tag;
        }
        
        return $tags;
    }
    
    /**
     * Saves tag data to database
     *
     * @param mixed $tag
     * 
     * @return int
     */
    public function save($tag,$db=null)
    {
        if (is_object($tag) && ($tag instanceof Application_Model_Tags))
        {
            $tag=$tag->toArray();
        }
        
        if (is_array($tag))
        {
            if (empty($db))
                $tag_tbl=new $this->_table_class();
            else
                $tag_tbl=new $this->_table_class(array('db'=>$db));
            
            if (!empty($tag['id']))
            {
                return $tag_tbl->update($tag,array('id=?'=>$tag['id']));
            } else 
            {
                return $tag_tbl->insert($tag);
            }
        }
    }
    
    /**
     * Deletes tag from database
     *
     * @param mixed $tag
     * @return int
     */
    public function delete($tag)
    {
        if (is_object($tag) && ($tag instanceof Application_Model_Tags))
        {
            $tag=$tag->toArray();
        }
        
        if (is_array($tag) && !empty($tag['id']))
        {
            $tag_tbl=new $this->_table_class();

            return $tag_tbl->delete(array('id=?'=>$tag['id']));
        }
    }
    
    /**
     * Deletes tags by node ID
     *
     * @param int $nid
     * @return int
     */
    public function deleteByNid($nid)
    {
        if (!empty($nid))
        {
            $tag_tbl=new $this->_table_class();

            return $tag_tbl->delete(array('nid=?'=>$nid));
        }
    }
    
    /**
     * Returns tags paginator
     *
     * @param int $page
     * @param int $limit
     * 
     * @return array
     */
    public function getTagsPage($page=1,$limit=10)
    {
        $tags=array();
        
        $db=Zend_Registry::get('db');
        
        $adapter = new Zend_Paginator_Adapter_DbSelect($db->select()
                                                          ->from(Application_Model_Tags::DB_TABLE)
                                                          ->where('language=?',Zend_Registry::get('language')->getLocale())
                                                          ->order(array('id DESC')));
        
        $rowCount = $db->select()->from(Application_Model_Tags::DB_TABLE)
            ->reset(Zend_Db_Select::COLUMNS)
            ->columns(new Zend_Db_Expr('COUNT(*) AS '.$db->quoteIdentifier(Zend_Paginator_Adapter_DbSelect::ROW_COUNT_COLUMN)))
            ->where('language=?',Zend_Registry::get('language')->getLocale());
    
        
        $adapter->setRowCount($rowCount);
        
        $paginator = new Zend_Paginator($adapter);
        $paginator->setCurrentPageNumber((int) $page);
        $paginator->setItemCountPerPage($limit);
        
        if (count($paginator))
        {
            foreach ($paginator as $row)
            {
                $tag=new Application_Model_Tags($row);
            
                $tags[]=$tag;
            }
        }
        
        return array($tags,$paginator);
    }
}

